#include "loginwidget.h"
#include "phoneloginwidget.h"


LoginWidget::LoginWidget(QWidget *parent)
    : QWidget{parent}
{
    //1.设置属性和布局管理器
    this->setWindowTitle("登录");
    this->setWindowIcon(QIcon(":/myResource/Image/WeChat.svg"));
    this->setFixedSize(300,300);
    this->setAttribute(Qt::WA_DeleteOnClose);

    QGridLayout* layout = new QGridLayout(this);
    layout->setSpacing(0);

    //2.添加控件
    QFont font("微软雅黑",30,400);

    titleTag = new QLabel;
    titleTag->setStyleSheet("QLabel{color:rgb(100,100,100);}");
    titleTag->setFixedHeight(50);
    titleTag->setText("登录");
    titleTag->setAlignment(Qt::AlignHCenter|Qt::AlignTop);
    titleTag->setFont(font);

    nameEidt = new QLineEdit;
    pswdEidt = new QLineEdit;
    verifyCodeEidt = new QLineEdit;

    auto handleEdit = [](QLineEdit* edit,const QString& text)
    {
        edit->setPlaceholderText(text);
        edit->setFixedHeight(30);
        edit->setStyleSheet("QLineEdit{border:none;border-radius:5px;padding-left:5px;}");
    };
    handleEdit(nameEidt,"输入用户名");
    handleEdit(pswdEidt,"输入密码");
    handleEdit(verifyCodeEidt,"输入验证码");

    verifyCodeWidget= new VerifyCodeWidget;

    submitBtn = new QPushButton;
    submitBtn->setText("登录");
    submitBtn->setFixedHeight(40);
    QString style = "QPushButton{border:1px solid grey;background-color:rgb(44, 182, 61);border-radius:5px;}";
    style+="QPushButton:hover{background-color:rgb(34, 172, 51);}";
    style+="QPushButton:pressed{background-color:rgb(24, 162, 41);}";
    submitBtn->setStyleSheet(style);

    switchModBtn = new QPushButton;
    switchModBtn->setText("注册");
    switchModBtn->setStyleSheet("QPushButton{border:none;}");
    switchPhoneModBtn = new QPushButton;
    switchPhoneModBtn->setText("手机号登录");
    switchPhoneModBtn->setStyleSheet("QPushButton{border:none;}");

    layout->addWidget(titleTag,0,0,1,3);
    layout->addWidget(nameEidt,1,0,1,3);
    layout->addWidget(pswdEidt,2,0,1,3);
    layout->addWidget(verifyCodeEidt,3,0,1,2);
    layout->addWidget(verifyCodeWidget,3,2,1,1);
    layout->addWidget(submitBtn,4,0,1,3);
    layout->addWidget(switchModBtn,5,0,1,1);
    layout->addWidget(switchPhoneModBtn,5,2,1,1);

    //3.设置信号和槽
    connect(switchModBtn,&QPushButton::clicked,this,&LoginWidget::switchMode);
    connect(submitBtn,&QPushButton::clicked,this,[=]()
    {
        //1.取出数据
        const QString& name = nameEidt->text();
        const QString& pswd = pswdEidt->text();
        const QString& VerifyCode = verifyCodeEidt->text();

        if(name.isEmpty()||pswd.isEmpty()||VerifyCode.isEmpty())
        {
            Toast::showMessage("输入账号密码错误!");
            verifyCodeWidget->refreshVerifyCode();
            verifyCodeEidt->clear();
            return;
        }

        //2.对比验证码是否正确

        if(!verifyCodeWidget->isRight(VerifyCode))
        {
            Toast::showMessage("输入验证码错误!");
            verifyCodeWidget->refreshVerifyCode();
            verifyCodeEidt->clear();
            return;
        }

        //3.发送网络请求
        model::DataCenter* dataCenter = model::DataCenter::getInstance();
        if(isLoginMod)
        {
            connect(dataCenter,&model::DataCenter::userLoginDone,this,&LoginWidget::userLoginDone);
            dataCenter->userLoginAsync(name,pswd);
        }
        else
        {
            connect(dataCenter,&model::DataCenter::userRegisterDone,this,&LoginWidget::userRegisterDone);
            dataCenter->userRegisterAsync(name,pswd);
        }

    });
    connect(switchPhoneModBtn,&QPushButton::clicked,this,[=]()
    {
        PhoneLoginWidget* phoneLoginWidget = new PhoneLoginWidget(isLoginMod);
        phoneLoginWidget->show();
        this->close();
    });

}

void LoginWidget::switchMode()
{
    if(isLoginMod)
    {
        titleTag->setText("注册");
        submitBtn->setText("注册");
        switchModBtn->setText("登录");
        switchPhoneModBtn->setText("手机号注册");
        this->setWindowTitle("注册");
    }
    else
    {
        titleTag->setText("登录");
        submitBtn->setText("登录");
        switchModBtn->setText("注册");
        switchPhoneModBtn->setText("手机号登录");
        this->setWindowTitle("登录");
    }
    isLoginMod^=1;
}

void LoginWidget::userLoginDone()
{
    MainWidget::getInstance()->show();
    this->close();
}

void LoginWidget::userRegisterDone()
{
    Toast::showMessage("注册成功");
    verifyCodeEidt->clear();
    switchMode();
}
